home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-portable.exe / quodlibet-3.3.0-portable / data / bin / wave.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  19KB  |  541 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. """Stuff to parse WAVE files.
  5.  
  6. Usage.
  7.  
  8. Reading WAVE files:
  9.       f = wave.open(file, 'r')
  10. where file is either the name of a file or an open file pointer.
  11. The open file pointer must have methods read(), seek(), and close().
  12. When the setpos() and rewind() methods are not used, the seek()
  13. method is not  necessary.
  14.  
  15. This returns an instance of a class with the following public methods:
  16.       getnchannels()  -- returns number of audio channels (1 for
  17.                          mono, 2 for stereo)
  18.       getsampwidth()  -- returns sample width in bytes
  19.       getframerate()  -- returns sampling frequency
  20.       getnframes()    -- returns number of audio frames
  21.       getcomptype()   -- returns compression type ('NONE' for linear samples)
  22.       getcompname()   -- returns human-readable version of
  23.                          compression type ('not compressed' linear samples)
  24.       getparams()     -- returns a tuple consisting of all of the
  25.                          above in the above order
  26.       getmarkers()    -- returns None (for compatibility with the
  27.                          aifc module)
  28.       getmark(id)     -- raises an error since the mark does not
  29.                          exist (for compatibility with the aifc module)
  30.       readframes(n)   -- returns at most n frames of audio
  31.       rewind()        -- rewind to the beginning of the audio stream
  32.       setpos(pos)     -- seek to the specified position
  33.       tell()          -- return the current position
  34.       close()         -- close the instance (make it unusable)
  35. The position returned by tell() and the position given to setpos()
  36. are compatible and have nothing to do with the actual position in the
  37. file.
  38. The close() method is called automatically when the class instance
  39. is destroyed.
  40.  
  41. Writing WAVE files:
  42.       f = wave.open(file, 'w')
  43. where file is either the name of a file or an open file pointer.
  44. The open file pointer must have methods write(), tell(), seek(), and
  45. close().
  46.  
  47. This returns an instance of a class with the following public methods:
  48.       setnchannels(n) -- set the number of channels
  49.       setsampwidth(n) -- set the sample width
  50.       setframerate(n) -- set the frame rate
  51.       setnframes(n)   -- set the number of frames
  52.       setcomptype(type, name)
  53.                       -- set the compression type and the
  54.                          human-readable compression type
  55.       setparams(tuple)
  56.                       -- set all parameters at once
  57.       tell()          -- return current position in output file
  58.       writeframesraw(data)
  59.                       -- write audio frames without pathing up the
  60.                          file header
  61.       writeframes(data)
  62.                       -- write audio frames and patch up the file header
  63.       close()         -- patch up the file header and close the
  64.                          output file
  65. You should set the parameters before the first writeframesraw or
  66. writeframes.  The total number of frames does not need to be set,
  67. but when it is set to the correct value, the header does not have to
  68. be patched up.
  69. It is best to first set all parameters, perhaps possibly the
  70. compression type, and then write audio frames using writeframesraw.
  71. When all frames have been written, either call writeframes('') or
  72. close() to patch up the sizes in the header.
  73. The close() method is called automatically when the class instance
  74. is destroyed.
  75. """
  76. import __builtin__
  77. __all__ = [
  78.     'open',
  79.     'openfp',
  80.     'Error']
  81.  
  82. class Error(Exception):
  83.     pass
  84.  
  85. WAVE_FORMAT_PCM = 1
  86. _array_fmts = (None, 'b', 'h', None, 'i')
  87. import struct
  88. if struct.pack('h', 1) == '\x00\x01':
  89.     big_endian = 1
  90. else:
  91.     big_endian = 0
  92. from chunk import Chunk
  93.  
  94. class Wave_read:
  95.     """Variables used in this class:
  96.  
  97.     These variables are available to the user though appropriate
  98.     methods of this class:
  99.     _file -- the open file with methods read(), close(), and seek()
  100.               set through the __init__() method
  101.     _nchannels -- the number of audio channels
  102.               available through the getnchannels() method
  103.     _nframes -- the number of audio frames
  104.               available through the getnframes() method
  105.     _sampwidth -- the number of bytes per audio sample
  106.               available through the getsampwidth() method
  107.     _framerate -- the sampling frequency
  108.               available through the getframerate() method
  109.     _comptype -- the AIFF-C compression type ('NONE' if AIFF)
  110.               available through the getcomptype() method
  111.     _compname -- the human-readable AIFF-C compression type
  112.               available through the getcomptype() method
  113.     _soundpos -- the position in the audio stream
  114.               available through the tell() method, set through the
  115.               setpos() method
  116.  
  117.     These variables are used internally only:
  118.     _fmt_chunk_read -- 1 iff the FMT chunk has been read
  119.     _data_seek_needed -- 1 iff positioned correctly in audio
  120.               file for readframes()
  121.     _data_chunk -- instantiation of a chunk class for the DATA chunk
  122.     _framesize -- size of one frame in the file
  123.     """
  124.     
  125.     def initfp(self, file):
  126.         self._convert = None
  127.         self._soundpos = 0
  128.         self._file = Chunk(file, bigendian = 0)
  129.         if self._file.getname() != 'RIFF':
  130.             raise Error, 'file does not start with RIFF id'
  131.         if self._file.read(4) != 'WAVE':
  132.             raise Error, 'not a WAVE file'
  133.         self._fmt_chunk_read = 0
  134.         self._data_chunk = None
  135.         while None:
  136.             self._data_seek_needed = 1
  137.             
  138.             try:
  139.                 chunk = Chunk(self._file, bigendian = 0)
  140.             except EOFError:
  141.                 break
  142.  
  143.             chunkname = chunk.getname()
  144.             if chunkname == 'fmt ':
  145.                 self._read_fmt_chunk(chunk)
  146.                 self._fmt_chunk_read = 1
  147.             elif chunkname == 'data':
  148.                 if not self._fmt_chunk_read:
  149.                     raise Error, 'data chunk before fmt chunk'
  150.                 self._data_chunk = chunk
  151.                 self._nframes = chunk.chunksize // self._framesize
  152.                 self._data_seek_needed = 0
  153.                 break
  154.             continue
  155.             if not (self._fmt_chunk_read) or not (self._data_chunk):
  156.                 raise Error, 'fmt chunk and/or data chunk missing'
  157.             return None
  158.  
  159.     
  160.     def __init__(self, f):
  161.         self._i_opened_the_file = None
  162.         if isinstance(f, basestring):
  163.             f = __builtin__.open(f, 'rb')
  164.             self._i_opened_the_file = f
  165.         
  166.         try:
  167.             self.initfp(f)
  168.         except:
  169.             if self._i_opened_the_file:
  170.                 f.close()
  171.             raise 
  172.  
  173.  
  174.     
  175.     def __del__(self):
  176.         self.close()
  177.  
  178.     
  179.     def getfp(self):
  180.         return self._file
  181.  
  182.     
  183.     def rewind(self):
  184.         self._data_seek_needed = 1
  185.         self._soundpos = 0
  186.  
  187.     
  188.     def close(self):
  189.         if self._i_opened_the_file:
  190.             self._i_opened_the_file.close()
  191.             self._i_opened_the_file = None
  192.         self._file = None
  193.  
  194.     
  195.     def tell(self):
  196.         return self._soundpos
  197.  
  198.     
  199.     def getnchannels(self):
  200.         return self._nchannels
  201.  
  202.     
  203.     def getnframes(self):
  204.         return self._nframes
  205.  
  206.     
  207.     def getsampwidth(self):
  208.         return self._sampwidth
  209.  
  210.     
  211.     def getframerate(self):
  212.         return self._framerate
  213.  
  214.     
  215.     def getcomptype(self):
  216.         return self._comptype
  217.  
  218.     
  219.     def getcompname(self):
  220.         return self._compname
  221.  
  222.     
  223.     def getparams(self):
  224.         return (self.getnchannels(), self.getsampwidth(), self.getframerate(), self.getnframes(), self.getcomptype(), self.getcompname())
  225.  
  226.     
  227.     def getmarkers(self):
  228.         pass
  229.  
  230.     
  231.     def getmark(self, id):
  232.         raise Error, 'no marks'
  233.  
  234.     
  235.     def setpos(self, pos):
  236.         if pos < 0 or pos > self._nframes:
  237.             raise Error, 'position not in range'
  238.         self._soundpos = pos
  239.         self._data_seek_needed = 1
  240.  
  241.     
  242.     def readframes(self, nframes):
  243.         if self._data_seek_needed:
  244.             self._data_chunk.seek(0, 0)
  245.             pos = self._soundpos * self._framesize
  246.             if pos:
  247.                 self._data_chunk.seek(pos, 0)
  248.             self._data_seek_needed = 0
  249.         if nframes == 0:
  250.             return ''
  251.         if None._sampwidth > 1 and big_endian:
  252.             import array as array
  253.             chunk = self._data_chunk
  254.             data = array.array(_array_fmts[self._sampwidth])
  255.             if not data.itemsize == self._sampwidth:
  256.                 raise AssertionError
  257.             nitems = None * self._nchannels
  258.             if nitems * self._sampwidth > chunk.chunksize - chunk.size_read:
  259.                 nitems = (chunk.chunksize - chunk.size_read) / self._sampwidth
  260.             data.fromfile(chunk.file.file, nitems)
  261.             chunk.size_read = chunk.size_read + nitems * self._sampwidth
  262.             chunk = chunk.file
  263.             chunk.size_read = chunk.size_read + nitems * self._sampwidth
  264.             data.byteswap()
  265.             data = data.tostring()
  266.         else:
  267.             data = self._data_chunk.read(nframes * self._framesize)
  268.         if self._convert and data:
  269.             data = self._convert(data)
  270.         self._soundpos = self._soundpos + len(data) // self._nchannels * self._sampwidth
  271.         return data
  272.  
  273.     
  274.     def _read_fmt_chunk(self, chunk):
  275.         (wFormatTag, self._nchannels, self._framerate, dwAvgBytesPerSec, wBlockAlign) = struct.unpack('<HHLLH', chunk.read(14))
  276.         if wFormatTag == WAVE_FORMAT_PCM:
  277.             sampwidth = struct.unpack('<H', chunk.read(2))[0]
  278.             self._sampwidth = (sampwidth + 7) // 8
  279.         else:
  280.             raise Error, 'unknown format: %r' % (wFormatTag,)
  281.         self._framesize = None._nchannels * self._sampwidth
  282.         self._comptype = 'NONE'
  283.         self._compname = 'not compressed'
  284.  
  285.  
  286.  
  287. class Wave_write:
  288.     """Variables used in this class:
  289.  
  290.     These variables are user settable through appropriate methods
  291.     of this class:
  292.     _file -- the open file with methods write(), close(), tell(), seek()
  293.               set through the __init__() method
  294.     _comptype -- the AIFF-C compression type ('NONE' in AIFF)
  295.               set through the setcomptype() or setparams() method
  296.     _compname -- the human-readable AIFF-C compression type
  297.               set through the setcomptype() or setparams() method
  298.     _nchannels -- the number of audio channels
  299.               set through the setnchannels() or setparams() method
  300.     _sampwidth -- the number of bytes per audio sample
  301.               set through the setsampwidth() or setparams() method
  302.     _framerate -- the sampling frequency
  303.               set through the setframerate() or setparams() method
  304.     _nframes -- the number of audio frames written to the header
  305.               set through the setnframes() or setparams() method
  306.  
  307.     These variables are used internally only:
  308.     _datalength -- the size of the audio samples written to the header
  309.     _nframeswritten -- the number of frames actually written
  310.     _datawritten -- the size of the audio samples actually written
  311.     """
  312.     
  313.     def __init__(self, f):
  314.         self._i_opened_the_file = None
  315.         if isinstance(f, basestring):
  316.             f = __builtin__.open(f, 'wb')
  317.             self._i_opened_the_file = f
  318.         
  319.         try:
  320.             self.initfp(f)
  321.         except:
  322.             if self._i_opened_the_file:
  323.                 f.close()
  324.             raise 
  325.  
  326.  
  327.     
  328.     def initfp(self, file):
  329.         self._file = file
  330.         self._convert = None
  331.         self._nchannels = 0
  332.         self._sampwidth = 0
  333.         self._framerate = 0
  334.         self._nframes = 0
  335.         self._nframeswritten = 0
  336.         self._datawritten = 0
  337.         self._datalength = 0
  338.         self._headerwritten = False
  339.  
  340.     
  341.     def __del__(self):
  342.         self.close()
  343.  
  344.     
  345.     def setnchannels(self, nchannels):
  346.         if self._datawritten:
  347.             raise Error, 'cannot change parameters after starting to write'
  348.         if nchannels < 1:
  349.             raise Error, 'bad # of channels'
  350.         self._nchannels = nchannels
  351.  
  352.     
  353.     def getnchannels(self):
  354.         if not self._nchannels:
  355.             raise Error, 'number of channels not set'
  356.         return self._nchannels
  357.  
  358.     
  359.     def setsampwidth(self, sampwidth):
  360.         if self._datawritten:
  361.             raise Error, 'cannot change parameters after starting to write'
  362.         if sampwidth < 1 or sampwidth > 4:
  363.             raise Error, 'bad sample width'
  364.         self._sampwidth = sampwidth
  365.  
  366.     
  367.     def getsampwidth(self):
  368.         if not self._sampwidth:
  369.             raise Error, 'sample width not set'
  370.         return self._sampwidth
  371.  
  372.     
  373.     def setframerate(self, framerate):
  374.         if self._datawritten:
  375.             raise Error, 'cannot change parameters after starting to write'
  376.         if framerate <= 0:
  377.             raise Error, 'bad frame rate'
  378.         self._framerate = framerate
  379.  
  380.     
  381.     def getframerate(self):
  382.         if not self._framerate:
  383.             raise Error, 'frame rate not set'
  384.         return self._framerate
  385.  
  386.     
  387.     def setnframes(self, nframes):
  388.         if self._datawritten:
  389.             raise Error, 'cannot change parameters after starting to write'
  390.         self._nframes = nframes
  391.  
  392.     
  393.     def getnframes(self):
  394.         return self._nframeswritten
  395.  
  396.     
  397.     def setcomptype(self, comptype, compname):
  398.         if self._datawritten:
  399.             raise Error, 'cannot change parameters after starting to write'
  400.         if comptype not in ('NONE',):
  401.             raise Error, 'unsupported compression type'
  402.         self._comptype = comptype
  403.         self._compname = compname
  404.  
  405.     
  406.     def getcomptype(self):
  407.         return self._comptype
  408.  
  409.     
  410.     def getcompname(self):
  411.         return self._compname
  412.  
  413.     
  414.     def setparams(self, params):
  415.         (nchannels, sampwidth, framerate, nframes, comptype, compname) = params
  416.         if self._datawritten:
  417.             raise Error, 'cannot change parameters after starting to write'
  418.         self.setnchannels(nchannels)
  419.         self.setsampwidth(sampwidth)
  420.         self.setframerate(framerate)
  421.         self.setnframes(nframes)
  422.         self.setcomptype(comptype, compname)
  423.  
  424.     
  425.     def getparams(self):
  426.         if not (self._nchannels) and not (self._sampwidth) or not (self._framerate):
  427.             raise Error, 'not all parameters set'
  428.         return (self._nchannels, self._sampwidth, self._framerate, self._nframes, self._comptype, self._compname)
  429.  
  430.     
  431.     def setmark(self, id, pos, name):
  432.         raise Error, 'setmark() not supported'
  433.  
  434.     
  435.     def getmark(self, id):
  436.         raise Error, 'no marks'
  437.  
  438.     
  439.     def getmarkers(self):
  440.         pass
  441.  
  442.     
  443.     def tell(self):
  444.         return self._nframeswritten
  445.  
  446.     
  447.     def writeframesraw(self, data):
  448.         self._ensure_header_written(len(data))
  449.         nframes = len(data) // self._sampwidth * self._nchannels
  450.         if self._convert:
  451.             data = self._convert(data)
  452.         if self._sampwidth > 1 and big_endian:
  453.             import array
  454.             data = array.array(_array_fmts[self._sampwidth], data)
  455.             if not data.itemsize == self._sampwidth:
  456.                 raise AssertionError
  457.             None.byteswap()
  458.             data.tofile(self._file)
  459.             self._datawritten = self._datawritten + len(data) * self._sampwidth
  460.         else:
  461.             self._file.write(data)
  462.             self._datawritten = self._datawritten + len(data)
  463.         self._nframeswritten = self._nframeswritten + nframes
  464.  
  465.     
  466.     def writeframes(self, data):
  467.         self.writeframesraw(data)
  468.         if self._datalength != self._datawritten:
  469.             self._patchheader()
  470.  
  471.     
  472.     def close(self):
  473.         if self._file:
  474.             
  475.             try:
  476.                 self._ensure_header_written(0)
  477.                 if self._datalength != self._datawritten:
  478.                     self._patchheader()
  479.                 self._file.flush()
  480.             finally:
  481.                 self._file = None
  482.  
  483.         if self._i_opened_the_file:
  484.             self._i_opened_the_file.close()
  485.             self._i_opened_the_file = None
  486.  
  487.     
  488.     def _ensure_header_written(self, datasize):
  489.         if not self._headerwritten:
  490.             if not self._nchannels:
  491.                 raise Error, '# channels not specified'
  492.             if not self._sampwidth:
  493.                 raise Error, 'sample width not specified'
  494.             if not self._framerate:
  495.                 raise Error, 'sampling rate not specified'
  496.             self._write_header(datasize)
  497.  
  498.     
  499.     def _write_header(self, initlength):
  500.         if not not (self._headerwritten):
  501.             raise AssertionError
  502.         None._file.write('RIFF')
  503.         if not self._nframes:
  504.             self._nframes = initlength / self._nchannels * self._sampwidth
  505.         self._datalength = self._nframes * self._nchannels * self._sampwidth
  506.         self._form_length_pos = self._file.tell()
  507.         self._file.write(struct.pack('<L4s4sLHHLLHH4s', 36 + self._datalength, 'WAVE', 'fmt ', 16, WAVE_FORMAT_PCM, self._nchannels, self._framerate, self._nchannels * self._framerate * self._sampwidth, self._nchannels * self._sampwidth, self._sampwidth * 8, 'data'))
  508.         self._data_length_pos = self._file.tell()
  509.         self._file.write(struct.pack('<L', self._datalength))
  510.         self._headerwritten = True
  511.  
  512.     
  513.     def _patchheader(self):
  514.         if not self._headerwritten:
  515.             raise AssertionError
  516.         if None._datawritten == self._datalength:
  517.             return None
  518.         curpos = None._file.tell()
  519.         self._file.seek(self._form_length_pos, 0)
  520.         self._file.write(struct.pack('<L', 36 + self._datawritten))
  521.         self._file.seek(self._data_length_pos, 0)
  522.         self._file.write(struct.pack('<L', self._datawritten))
  523.         self._file.seek(curpos, 0)
  524.         self._datalength = self._datawritten
  525.  
  526.  
  527.  
  528. def open(f, mode = None):
  529.     if mode is None:
  530.         if hasattr(f, 'mode'):
  531.             mode = f.mode
  532.         else:
  533.             mode = 'rb'
  534.     if mode in ('r', 'rb'):
  535.         return Wave_read(f)
  536.     if None in ('w', 'wb'):
  537.         return Wave_write(f)
  538.     raise None, "mode must be 'r', 'rb', 'w', or 'wb'"
  539.  
  540. openfp = open
  541.